/**
 * Created by hzq16239 on 2016/1/22.
 */
var formidable = require('formidable');
var fs = require('fs');
var UploadImage = require('../models/UploadImage.js');
//companyIntro index
module.exports.index = function index(req,res,next){
    res.render('./servers/companyIntro',{imagesPath:[],none:'block'});
};


//company intro add inf
module.exports.addIntro = function addIntro(req,res,next){

};

module.exports.uploadImage = function uploadImage(req,res,next){
    var companyName = 'tc';
    //创建一个以公司命名的图片文件夹
    fs.exists('public/uploadImage/'+companyName+'/intro/',function(exists){
        if(!exists){
            fs.mkdir('public/uploadImage/'+companyName+'/intro/');
        }
    });
    var form = new formidable.IncomingForm();
        form.encoding = 'utf-8';
        form.uploadDir = 'public/uploadImage/'+companyName+'/intro/';
        form.keepExtensions = true;
        form.maxFieldsSize = 2*1024*1024;
    //files 里面的内容 会根据 input 的 name 显示， name 有几个就显示几个， 一样， 只显示最后一个
    form.parse(req,function(err,fields,files){
        if(err){
            console.log('formidable:' + err);
            res.send('fail');
        }else{
            var imgPath = [];
            var newType = [];
            var imgName = [];
                for( var index in files){
                    //console.log( typeof parseInt(index));
                     imgPath.push(files[parseInt(index)].path);
                    switch (files[parseInt(index)].type){
                        case 'image/png':newType.push('png');break;
                        case 'image/pjpeg':newType.push('jpg');break;
                        case 'image/jpeg':newType.push('jpg');break;
                        case 'image/x-png':newType.push('png');break;
                    }
                }
            for(var i=0;i<imgPath.length;i++){
                    var newName = form.uploadDir + Date.now()+'_'+ i + '.' + newType[i];
                     fs.rename(imgPath[i],newName);
                    var imgUri = newName.substring(newName.indexOf('/'),newName.length);
                //console.log(imgUri);
                    imgName.push(imgUri);

            }
            //把地址存入数据库，方便后面集中取出来

            UploadImage.find({name:companyName},function(err,data){
                if(err){
                    console.log('uploadImage db find' + err);
                }
                if(data.length){
                    var schemaJson = {};
                        schemaJson.imagePath = {};
                    var oldIntroImages =  data[0].imagePath.introImages;
                    imgName.forEach(function(imgName){
                        oldIntroImages.push(imgName);
                    });
                    //直接更新不做追加
                    schemaJson.imagePath.introImages = imgName;
                    schemaJson.imagePath.mapImages = data[0].imagePath.mapImages;
                    schemaJson.updateTime = new Date();
                    UploadImage.update(schemaJson,function(err){if(err)console.log('uploadImage db update' + err);});
                }else{
                    var schemaJsonCreate = {};
                    schemaJsonCreate.name = companyName;
                    schemaJsonCreate.imagePath = {};
                    schemaJsonCreate.imagePath.introImages = imgName;
                    schemaJsonCreate.imagePath.mapImages = [];
                    schemaJsonCreate.updateTime = new Date();
                    schemaJsonCreate.writeTime = new Date();
                    UploadImage.create(schemaJsonCreate,function(err){if(err) console.log('uploadImage create' + err)});
                }

            });
            res.render('./servers/uploadImage',{imagesPath:imgName,none:'none'});
        }
    })
}